package com.zhouchb.blog.service.impl;

import com.zhouchb.blog.NotFoundException;
import com.zhouchb.blog.bean.Blog;
import com.zhouchb.blog.dao.BlogRepository;
import com.zhouchb.blog.service.BlogService;
import com.zhouchb.blog.util.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author Zhouchb
 * @Create 2021-09-2021/9/7 :21:52
 * @Description
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogRepository repository;


    @Transactional
    @Override
    public int saveBlogService(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
        return repository.saveBlogDao(blog);
    }

    @Override
    public List<Blog> getAllBlogDao() {
        return repository.getAllBlogDao();
    }

    @Override
    public Blog getBlogByIdService(Long id) {
        return repository.getBlogDao(id);
    }

    @Override
    public Blog getBlogByIdConvert(Long id) {
        Blog blog = repository.getBlogDao(id);
        if (blog == null){
            throw new NotFoundException("该博客不存在");
        }
        String content = blog.getContent();
        blog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        return blog;
    }

    @Override
    public List<Blog> listBlogService(Blog blog) {
        return repository.listBlogDao(blog);
    }

    @Override
    public List<Blog> listRecommend(Integer size) {
        return repository.listRecommend(size);
    }

    @Transactional
    @Override
    public int updateBlogService(Long id, Blog blog) {
        blog.setUpdateTime(new Date());
        return repository.updateBlogDao(id, blog);
    }

    @Override
    public List<Blog> searchBlogService(String query) {
        return repository.searchBlog(query);
    }

    @Override
    public List<Blog> searchAllByTypeIdBlogsService(Long typeId) {
        return repository.searchAllByTypeIdBlogs(typeId);
    }

    @Override
    public List<Blog> searchPictureService(String pictureName) {
        return repository.searchPicture(pictureName);
    }

    @Override
    public List<Blog> listBlogByTagIdService(Long tagId) {
        return repository.listBlogByTagId(tagId);
    }

    @Transactional
    @Override
    public int delBlogService(Long id) {
        return repository.delBlogDao(id);
    }

    @Transactional
    @Override
    public int updateViews(Long blogId) {
        return repository.updateViews(blogId);
    }

    @Override
    public Map<String, List<Blog>> archivesBlog() {
        Map<String,List<Blog>> blogMap = new LinkedHashMap<>();
        List<String> listYear = repository.blogYear();
        for (int i=0;i<listYear.size();i++){
            blogMap.put(listYear.get(i),repository.listBlogByYear(listYear.get(i)));
            System.out.println(listYear.get(i));
        }
        return blogMap;
    }


    @Override
    public int blogTotalService() {
        return repository.blogTotal();
    }

}
